package com.sun.dailyprj;

import android.Manifest;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.sun.dailyprj.androidx.AndroidXActivity;
import com.sun.dailyprj.chery.CheryEntryActivity;
import com.sun.dailyprj.designpatterns.DesignPatternsActivity;
import com.sun.dailyprj.frame.FrameActivity;
import com.sun.dailyprj.generics.GenericsActivity;
import com.sun.dailyprj.junit.JunitMainActivity;
import com.sun.dailyprj.lazyfragment.LazyFragmentActivity;
import com.sun.dailyprj.navigation.NavigationActivity;
import com.sun.dailyprj.network.NetworkActivity;
import com.sun.dailyprj.pki.PkiActivity;
import com.sun.dailyprj.provider.ContentProviderActivity;
import com.sun.dailyprj.receiver.ReceiverActivity;
import com.sun.dailyprj.stream.ActivityStream;
import com.sun.dailyprj.test.TestActivity;
import com.sun.dailyprj.thread.ThreadActivity;
import com.sun.dailyprj.usb.USBActivity;
import com.sun.dailyprj.util.Logger;
import com.sun.dailyprj.view.ArcProgressActivity;
import com.sun.dailyprj.view.CustomViewActivity;
import com.sun.dailyprj.view.DividerActivity;
import com.sun.dailyprj.view.NonLineProgressActivity;
import com.sun.dailyprj.zotye.ui.CheckActivity;

import java.io.IOException;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

//        Logger.warn("mac:" + getMacAddress());
        test();
        execScript("am start -n com.carota.chery/.ui.EntryActivity");

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if(drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if(id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if(id == R.id.nav_camera) {
            // Handle the camera action
            startActivity(new Intent(this, JunitMainActivity.class));
        } else if(id == R.id.nav_patterns) {
//            startActivity(new Intent(this, DesignPatternsActivity.class));
            execScript("adb shell am start -n com.carota.chery/.ui.EntryActivity");

        } else if(id == R.id.nav_slideshow) {
            startActivity(new Intent(this, CustomViewActivity.class));
        } else if(id == R.id.nav_manage) {
            startActivity(new Intent(this, ThreadActivity.class));
        } else if(id == R.id.nav_share) {
            startActivity(new Intent(this, PkiActivity.class));
        } else if(id == R.id.nav_send) {
            startActivity(new Intent(this, FrameActivity.class));
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void startGenerics(View view) {
        startActivity(new Intent(this, GenericsActivity.class));
    }

    public void startHttp(View view) {
        startActivity(new Intent(this, NetworkActivity.class));
    }

    public void startLazyFragment(View view) {
        startActivity(new Intent(this, LazyFragmentActivity.class));
    }

    @Override
    protected void onResume() {
        super.onResume();
        verifyStoragePermissions(this);
        execScript("am start -n com.carota.chery/.ui.EntryActivity");
        Intent i = new Intent();
        i.setComponent(new ComponentName("com.carota.chery", "com.carota.chery.ui.EntryActivity"));
//        startActivity(i);
    }

    public static void verifyStoragePermissions(AppCompatActivity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if(permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }
    }

    public void startNavigation(View view) {
        startActivity(new Intent(this, NavigationActivity.class));
    }

    public void startCompat(View view) {
        startActivity(new Intent(this, AndroidXActivity.class));
    }

    public void startReceiver(View view) {
        startActivity(new Intent(this, ReceiverActivity.class));

    }


    public void startFileDescriptor(View view) {
        startActivity(new Intent(MainActivity.this, ActivityStream.class));
    }

    public void startUsb(View view) {
        this.startActivity(new Intent(this, USBActivity.class));
    }

    public void zotye(View view) {
        this.startActivity(new Intent(this, CheckActivity.class));
    }

    public void testEnum(View view) {
        this.startActivity(new Intent(this, TestActivity.class));
    }

    public void cheryOnClick(View view) {
        startActivity(new Intent(this, CheryEntryActivity.class));
    }

    public void testContentProvider(View view) {
        startActivity(new Intent(this, ContentProviderActivity.class));
    }

    public void nonLineOnClick(View view){
        startActivity(new Intent(this, NonLineProgressActivity.class));
    }

    public void arcProgress(View view) {
        startActivity(new Intent(this, ArcProgressActivity.class));
    }

    public void onViewClick(View view) {
        startActivity(new Intent(this, DividerActivity.class));
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private String getMacAddress() {
        try {
            DeviceAdminReceiver admin = new DeviceAdminReceiver();
            DevicePolicyManager devicepolicymanager = admin.getManager(getApplicationContext());
            ComponentName name1 = admin.getWho(getApplicationContext());
            if (devicepolicymanager.isAdminActive(name1)){
                String mac_address = devicepolicymanager.getWifiMacAddress(name1);
                Log.e("macAddress",""+mac_address);
            }
            DevicePolicyManager devicePolicyManager =
                    (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
            ComponentName cn = new ComponentName(MainActivity.this, MainActivity.class);
            String mac = devicePolicyManager.getWifiMacAddress(cn);
            return mac;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void test (){
        Method method = null;
        Method[] methods = WifiManager.class.getMethods();
        WifiManager mWifiManager;
        try {
            mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            for ( Method m : methods ) {
                if ( "getFactoryMacAddresses".equals(m.getName()) ) {
                    method = m;
                    Logger.debug("getFactoryMacAddresses 找到方法了");
                    String[] macAddresses  = (String[]) method.invoke(mWifiManager, new Object[0]);
                    String macAddress = null;
                    if (macAddresses != null && macAddresses.length > 0) {
                        macAddress = macAddresses[0];
                        Logger.debug("macAddress--"+macAddress);
                    }else{
                        Logger.debug("macAddress-- error");
                    }
                }
            }
            Logger.debug("循环完毕  getFactoryMacAddresses");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean execScript(String command) {
        try {
            java.lang.Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
            return process.exitValue() == 0;
        } catch (IOException | InterruptedException e) {
            Logger.error(e);
        }
        return false;
    }
}
